package project02.service;


import project02.domain.Job;
import project02.domain.User;
import refelct.Student;

import javax.sound.midi.Soundbank;
import java.io.*;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Properties;
import java.util.Scanner;


public class UserListService {

    public final static String UID_NAME = "src/project02/information/uidName.txt";
    public final static String INFORMATION = "src/project02/information/userInformation.txt";


    public static void myLoad() throws IOException {
        File file=new File(UID_NAME);
        if (!file.exists()){
            file.createNewFile();
        }

        Properties prop = new Properties();
        FileReader fr = new FileReader(UID_NAME);
        prop.load(fr);
        fr.close();
    }

    public static void myStore() throws IOException {
        Properties prop = new Properties();

        prop.setProperty("tea01","刘磊");
        prop.setProperty("stu01","李佳澳");
        prop.setProperty("tea02","魈");
        prop.setProperty("stu02","张毅");
        prop.setProperty("tea03","林青霞");
        prop.setProperty("stu03","惠爱玉");
        prop.setProperty("tea04","李嘉欣");
        prop.setProperty("stu04","惠爱玉");

        FileWriter fw = new FileWriter(UID_NAME);
        prop.store(fw,null);
        fw.close();
    }

    public static void getAllUser() throws IOException, ClassNotFoundException {
        ObjectInputStream jis = new ObjectInputStream(new FileInputStream(INFORMATION));

        ArrayList<User> list  = (ArrayList<User>)jis.readObject();

        for (int i = 0; i < list.size(); i++ ){
            User j = list.get(i);
            System.out.println(j);
        }

        jis.close();

    }

    public static void addUser(String uid,String name) throws IOException, ClassNotFoundException {
        Properties prop = new Properties();
        FileReader fr = new FileReader(UID_NAME);
        prop.load(fr);
        if(prop.contains(uid)){
            System.out.println("用户已存在！");
        }else {

        prop.setProperty(uid,name);
        FileWriter fw = new FileWriter(UID_NAME);
        prop.store(fw,null);
            System.out.println("添加成功！");
        fw.close();}
        fr.close();


    }

    public static void modifyLevel(String uid1,Integer level1) throws IOException, ClassNotFoundException {
        ObjectInputStream jis = new ObjectInputStream(new FileInputStream(INFORMATION));

        ArrayList<User> list  = (ArrayList<User>)jis.readObject();

        for (User user : list) {
            if (user.getUid().equals(uid1)){
                user.setLevel(level1);
            }else {
                System.out.println("未查询到该用户，请联系管理员。");
            }
        }
        ObjectOutputStream ois=new ObjectOutputStream(new FileOutputStream(INFORMATION));
        ois.writeObject(list);
        jis.close();
        ois.flush();
        ois.close();


        }

    public static void deleteUser(String uid2) throws IOException, ClassNotFoundException {
        boolean flag=true;
      ObjectInputStream ins=new ObjectInputStream(new FileInputStream(INFORMATION));
      ArrayList<User>fr=(ArrayList<User>)ins.readObject();
        for (User user : fr) {
            if (user.getUid().equals(uid2)){
                fr.remove(user);
                flag=false;
                break;
            }

        }if (flag){
            System.out.println("未查询到该用户！");
        }else {
        ObjectOutputStream ois=new ObjectOutputStream(new FileOutputStream(INFORMATION));
        ois.writeObject(fr);
        ois.flush();
        ois.close();
        }}



    public static void getPasswordMenu(String name, Integer uid) throws IOException, ClassNotFoundException {
        ObjectInputStream sio=new ObjectInputStream(new FileInputStream(INFORMATION));
        ArrayList<User>list=(ArrayList<User>)sio.readObject();
        for (User user : list) {
            if (user.getName().equals(name)&&user.getUid().equals(uid)){
                System.out.println("匹配成功，请重设密码：");
                Scanner sc=new Scanner(System.in);
                String newPassWord=sc.next();
                user.setPassword(newPassWord);
                System.out.println("密码已经重设，请重新登陆。");
                break;

            }else {
                System.out.println("未查询到该用户！");

            }

        }
        sio.close();
    }


    //管理员查看指定uid的用户信息
    public void getUidUser(String uid) throws IOException, ClassNotFoundException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入想要查看的uid： ");
         uid=sc.next();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(INFORMATION));
        Object obj = ois.readObject();
        User u=(User)obj;
        if(u.getUid().equals(uid)){
            System.out.println(u.getUid()+"\t"+u.getName());
        }else {
            System.out.println("该用户不存在，请联系管理员处理或重新输入。");
        }
        ois.close();


    }



}




    /*{
        list.add(new User("1001","1234567", "李佳澳", 18, "男", 117452613, "软件工程学院",
                "1月1日", 1000000, "12月21日",5));
        list.add(new User("2001","456789", "刘磊", 20, "男", 1238452769, "计算机学院",
                "1月2日", 2000000, "8月1日",4));
        list.add(new User("1002","1111110", "魈", 18, "男", 111111111, "艺术学院",
                "1月3日", 99000000, "4月17日",3));
        list.add(new User("2002","7456789", "迦南", 18, "女", 22222222, "经济管理学院",
                "1月4日", 2000000, "9月1日",2));
        list.add(new User("1003","20000000", "张晓玲", 18, "女", 33333333, "数据科学与工程学院",
                "1月1日", 1000000, "8月25日",4));
        list.add(new User("2003","25121546", "张悦", 20, "男", 444444444, "计算机学院",
                "1月2日", 2000000, "6月1日",1));


        try {
            FileOutputStream fos = new FileOutputStream("/information/UserManagementList.txt");
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(list);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        }*/

    //管理员查看指定uid的用户
   /* public ArrayList<User>getList(){return list;}
    public User getList(Integer uid) throws UserException{
        for ( User us:list){//增强for循环
            if (us.getUid()==uid){
                return us;
            }
        }
        throw new UserException("找不到用户，请确认uid是否输入正确！");


    //管理员添加用户
    public void addUser(char a,Integer uid,String name){
        switch (a){
            case '1'://添加学生；
                list.add(new Student(list.size()+1,uid,name));
                break;
            case '2'://添加老师；
                list.add(new Teacher(list.size()+1,uid,name));
        }
    }
    //管理员修改用户
    public void modifyUser(User list, String name, Integer age, Integer uid,Integer credit_Rating){
        list.setName(name);
        list.setAge(age);
        list.setUid(uid);
        list.setCredit_Rating(credit_Rating);
    }
    //管理员删除用户
    public boolean deleteUser(Integer uid){
        try {
            list.remove(getList(uid));
        } catch (UserException e) {
            System.out.println(e.getMessage());
            return false;
    }
        for (int i=0;i<list.size();i++){
            list.get(i).setUid(i+1);}
        return true;
    }}*/
